﻿public class Solution {
    public string RemoveDuplicates(string s)
    {
        Stack<char> stack = new Stack<char>();

        for (int i = s.Length - 1; i >= 0; i--)
        {
            if (stack.Count == 0 || stack.Peek() != s[i])
            {
                stack.Push(s[i]);
            }
            else if (stack.Peek() == s[i])
            {
                stack.Pop();
            }
        }
        return new string(stack.ToArray());
    }

    public static void Main(string[] args)
    {
        string s = "abbaca";
        Solution solution = new Solution();
        string res = solution.RemoveDuplicates(s);
        Console.WriteLine(res);
    }
}